// pages/order/detail/detail.js
import {
  OrderModel
} from '../../../model/order'
import {
  config
} from '../../../config'
var order = new OrderModel()
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bottom: 0,
    info: {},
    id: 0,
    detail: {},
    count: 0,
    address: {},
    chooseOk: {},
    useFlower:0,
    flower: true,
    couponList: [],
    coupon: {},
    isAgent: 0,
    onLinePay: true,
    offLinePay: false,
    agentType:0,
    offlineOrNot:0
  },
  onLinePay() {
    this.setData({
      onLinePay: true,
      offLinePay: false,
      agentType:0
    })
  },
  offLinePay() {
    console.log(this.data.offlineOrNot)
  debugger;
    if(this.data.offlineOrNot != 0){
      this.setData({
        onLinePay: false,
        offLinePay: true,
        agentType:1
      })
    }
  },
  // 选择地址
  chooseAddress() {
    var that = this
    wx.navigateTo({
      url: '/pages/address/list/list?isChoose=' + 1,
      events: {
        addressInfo: function (data) {
          that.setData({
            address: data
          })
        },
      },
    })
  },

  flowerChange: function (e) {
    var that = this
    if(that.data.isAgent==1){
      var total = that.data.detail.skuModel.agentPrice*that.data.count
    }else{
      var total = that.data.detail.skuModel.retailPrice*that.data.count
    }
    if(e.detail.value){
      if(that.data.chooseOk>=total){
        this.setData({
          flower: e.detail.value,
          useFlower:total
        })
      }else{
        this.setData({
          flower: e.detail.value,
          useFlower:that.data.chooseOk
        })
      }
    }
    else{
      this.setData({
        flower: e.detail.value
      })
    }
  },

  sendOrder: function (e) {
    var data = this.data
    if (!data.address) {
      wx.showToast({
        title: '请选择地址',
        icon: "none"
      })
      return
    }
      order.order(data.address.id, data.detail.id, data.count, data.detail.skuModel.id, data.coupon.id, data.flower ? 1 : 0,data.agentType).then(
        res => {
          if (res.code === 0) {
            if(data.agentType==0){
              wx.navigateTo({
                url: '/pages/order/detail/detail?id=' + res.data,
              })
            }else{
              wx.showToast({
                title: '划卡成功等待代理商确认',
              })
              wx.navigateBack()
            }
          }
        }
      )
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      bottom: config.statusBarHeight,
      isAgent: app.globalData.userInfo.isAgent,
      offlineOrNot:app.globalData.userInfo.offlineOrNot
    })
    var that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('detailInfo', function (data) {
      that.setData({
        detail: data.model,
        count: data.count,
      })
      order.promptlyChooseOk(that.data.count,that.data.detail.skuModel.goodsId,that.data.detail.skuModel.id).then(
        res=>{
          that.setData({
            chooseOk:res.data.balancePrice
          })
        }
      )
      // order.conponList(data.model.id+"").then(
      //   res=>{
      //     that.setData({
      //       couponList:res.data
      //     })
      //   }
      // )
    })

    order.address().then(
      res => {
        this.setData({
          address: res.data.address
        })
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})